import O1HD from "../../engines/gameEngine";
import { CsvFileKind } from "../../engines/libs/CsvFileKind";
import level_peopleArchery from "../../levelBundles/level_PeopleArchery/level_peopleArchery";
import PageBase from "../Base/PageBase";
import mainRoot from "../MainRoot";

const {ccclass, property} = cc._decorator;

@ccclass
export default class peopleArcherySynthPage extends PageBase {

    @property({type:cc.Node,tooltip:"展示图片"})
    showNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"武器名字"})
    lbName:cc.Label = null;

    @property({type:cc.Label,tooltip:"属性文本"})
    lbParam:cc.Label = null;

    @property({type:cc.Label,tooltip:"合成文本"})
    lbSynth:cc.Label = null;

    weaponId:string = "";

    level:level_peopleArchery = null;

    oneWeaponId:string = "";
    twoWeaponId:string = "";

    bindLevel(level:level_peopleArchery,weaponId:string,oneWeaponId:string,twoWeaponId:string){
        this.weaponId = weaponId;
        this.oneWeaponId = oneWeaponId;
        this.twoWeaponId = twoWeaponId;
        this.level = level;
        this.loadPage();
    }

    loadPage(){
        let data = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryMagicWeapon,this.weaponId);
        let oneData = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryMagicWeapon,this.oneWeaponId);
        let twoData = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryMagicWeapon,this.twoWeaponId);
        let sp = this.showNode.getComponent(cc.Sprite);
        let iconPath:string = data["icon_path"];
        let name:string = data["name"];
        this.lbName.string = name;
        this.lbSynth.string = oneData["name"] + "+" + twoData["name"];
        this.lbParam.string = "";
        let param = data["param"];
            let pAry:string[] = param.split("&");
            let typeAry:string[] = [];
            let paramAry:number[] = [];
            pAry.forEach((p:string)=>{
                let strAry:string[] = p.split("|");
                typeAry.push(strAry["0"]);
                paramAry.push(Number(strAry["1"]));
            });

            for(let i =0; i<pAry.length;i++){
                let data = {param_type:typeAry[i],option_param:paramAry[i]};
                let chat = O1HD.getManagers().peopleArcheryManager.getOptionText(data);
                this.lbParam.string += chat + (i == 0 ? "\n" :"");
            }
            mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`selectWeapon/${iconPath}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                if(err){
                    console.log("加载道具icon失败",err);
                    return;
                }
                sp.spriteFrame = spf;
            });
    }

    protected update(dt: number): void {
        this.level.gamePauseFlag = true;
    }

    onDestroy(): void {
        this.level.gamePauseFlag = false;
    }

}
